<script lang="ts" setup>
import {
  IonContent,
  IonPage,
  IonItem,
  IonInput,
  IonButton,
  IonText,
  IonInputPasswordToggle,
  useIonRouter,
  IonSelect,
  IonSelectOption,
} from "@ionic/vue";
import { ref } from "vue";
import { registerUser, registerStudentClass } from "@/api/login";
import { useUserStore } from "@/stores";
import { alertController } from "@ionic/vue";
import { Class } from "@/types/public";

const router = useIonRouter();
const userStore = useUserStore();

const username = ref("");
const password = ref("");
const passwordConfirm = ref("");
const classList = ref<Class[]>([]);
const selectedClass = ref("");

const response = await registerStudentClass();
if (response.status) {
  classList.value = response.data;
}

const gotoLogin = () => {
  router.push("/login");
};
const register = async () => {
  // TODO: 验证用户名和密码是否为空
  if (!username.value || !password.value || !passwordConfirm.value) {
    const alert = await alertController.create({
      header: "错误",
      message: "用户名或密码不能为空",
      buttons: ["确定"],
    });
    await alert.present();
    return;
  }
  const user = await registerUser({
    username: username.value,
    password: password.value,
    role: 0,
    studentClassName: selectedClass.value,
  });
  userStore.setToken(user.data);
  // TODO: 跳转到首页
  router.push("/");
};
</script>

<template>
  <ion-page>
    <ion-content class="ion-padding">
      <div class="login-container">
        <img src="@/assets/login.png" alt="Logo" class="logo" />
        <h1>欢迎注册</h1>
        <ion-item>
          <ion-input v-model="username" label="用户名" type="text"></ion-input>
        </ion-item>
        <ion-item>
          <ion-select v-model="selectedClass" label="选择班级" placeholder="请选择班级">
            <ion-select-option v-for="myClass in classList" :key="myClass.className" :value="myClass.className">
              {{ myClass.className }}
            </ion-select-option>
          </ion-select>
        </ion-item>
        <ion-item>
          <ion-input v-model="password" label="密码" type="password">
            <ion-input-password-toggle slot="end"></ion-input-password-toggle>
          </ion-input>
        </ion-item>
        <ion-item>
          <ion-input v-model="passwordConfirm" label="密码" type="password">
            <ion-input-password-toggle slot="end"></ion-input-password-toggle>
          </ion-input>
        </ion-item>
        <ion-button expand="block" @click="register">注册</ion-button>
        <div class="additional-options">
          <ion-text color="medium">
            <span @click="gotoLogin">返回登录</span>
          </ion-text>
        </div>
      </div>
    </ion-content>
  </ion-page>
</template>

<style scoped>
.login-container {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 80%;
  max-width: 300px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  box-sizing: border-box;
}

/* 确保内容不会溢出容器 */
.login-container > * {
  max-width: 100%;
}

.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 20px;
}

h1 {
  font-size: 24px;
  margin-bottom: 30px;
  color: var(--ion-color-primary);
}

ion-item {
  --padding-start: 0;
  --inner-padding-end: 0;
  margin-bottom: 20px;
}

ion-button {
  margin-top: 30px;
  --border-radius: 25px;
  width: 100%;
  height: 40px; /* 增加高度 */
  font-size: 18px; /* 增加字体大小 */
}

.additional-options {
  margin-top: 20px;
  font-size: 14px;
}

.additional-options span {
  cursor: pointer;
}

.divider {
  margin: 0 10px;
}

.social-login {
  margin-top: 40px;
  text-align: center;
}

.social-icons {
  display: flex;
  justify-content: center;
  margin-top: 15px;
}

.social-icons ion-icon {
  font-size: 24px;
  margin: 0 10px;
  cursor: pointer;
  padding: 10px;
  border-radius: 50%;
  background-color: var(--ion-color-light);
}

/* 添加一些动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

ion-select {
  width: 100%;
  max-width: 100%;
  --padding-start: 0;
  --padding-end: 0;
}

ion-item {
  --padding-start: 0;
  --inner-padding-end: 0;
  margin-bottom: 20px;
  width: 100%;
}


.login-container > * {
  animation: fadeIn 0.5s ease-out forwards;
  opacity: 0;
}

.login-container > *:nth-child(1) {
  animation-delay: 0.1s;
}
.login-container > *:nth-child(2) {
  animation-delay: 0.2s;
}
.login-container > *:nth-child(3) {
  animation-delay: 0.3s;
}
.login-container > *:nth-child(4) {
  animation-delay: 0.4s;
}
.login-container > *:nth-child(5) {
  animation-delay: 0.5s;
}
.login-container > *:nth-child(6) {
  animation-delay: 0.6s;
}
</style>
